﻿using StaffingSystem.Business;
using StaffingSystem.EF;
using StaffingSystem.Models;
using StaffingSystem.Tool;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace StaffingSystem.Controllers
{
    public class ShowsController : Controller
    {
        Management db;
        // GET: Shows
        public ActionResult Lists()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Lists(PageViewModel page)
        {
            string where = "";
            Perpagingr userlist = new Perpagingr();//b层Personnelinfor
            if (page.Where != null)
            {
                BasicModel bo = new BasicModel();
                bo = JsonTool.JsonDeserialize<BasicModel>(page.Where);
                where+= "Name like'%" + bo.Name + "%'";
                if (bo.Intone== "已通过")
                {
                    where += "and Intone=1";

                }else if(bo.Intone== "未通过")
                {
                    where += "and Intone=2";
                }else if(bo.Intone == "未面试")
                {
                    where += "and Intone=3";
                }
            }
            PageModel<Viewone> pagemodel = new PageModel<Viewone>();
            pagemodel.PageSize = page.PageSize;
            pagemodel.PageNumber = page.PageNumber;
            pagemodel.AscOrDesc = "";
            pagemodel.OrderID = "Id";
            pagemodel.PageKey = "Id";
            pagemodel.TableName = "Viewone";
            pagemodel.Where = where;
            pagemodel = userlist.GetPageList(pagemodel);
            JsonResult result = new JsonResult();
            if (pagemodel.DataList.Count == 0)
            {
              result.Data = new
              {
                Sate = 0,
                Message = "没有记录",
                //Data = JsonTool.JsonSerializer(pagemodel)
                Data = ""
               };
            }
            else
            {
                pagemodel.Where = "";
                result.Data = new
                {
                    State = 1,
                    Message = "获取数据成功",
                    Data = JsonTool.JsonSerializer(pagemodel)
                };
            }
            return result;
        }
        //修改
        public ActionResult UpdateApprove(Viewone ta)
        {
            /// <summary>
            ///     <panel>
            ///           <input name = "Posts" title="同意" type="radio" checked="" value="通过">
            ///           <input name = "Posts" title="不同意" type="radio" value="拒绝">
            ///       </panel>
            /// </summary>
            /// 
            /// <param name="ta"></param>
            /// <returns></returns>

            int s = 0;
            if (ta.Name=="通过")// name = "Posts" value="通过"
            {
                s = 1;
            }  
            else if (ta.Name == "拒绝")//name = "Posts"   value="拒绝"
            {
                s = 2;
            }
            JsonResult result = new JsonResult();
            db = new Management();
            //使用拼接字符串的方式，将要执行的存储过程名称以及参数拼接在一起
            StringBuilder procString = new StringBuilder();
            procString.Append("PerInterview_update");
            procString.Append(" @ids,");
            procString.Append("@state");
            SqlParameter[] sq = {
                    new SqlParameter("@ids",ta.Id),
                    new SqlParameter("@state",s)
            };
            int a = db.Database.ExecuteSqlCommand(procString.ToString(),sq);
            Viewone n = db.Viewone.FirstOrDefault(d => d.Id == ta.Id);
            int c = n.Inttwo;

            if (a > 0)
            {
                if (c==1)
                {
                    Session["View_oneID"] = ta.Id;
                    result.Data = new
                    {
                        State = "3",
                        Message = "员工已通过所有面试,请为他安排职位!"
                    };
                }
                else
                {
                    result.Data = new
                    {
                        State = "1",
                        Message = "修改成功"
                    };
                }
            }
            else
            {
                result.Data = new
                {
                    State = "2",
                    Message = "修改失败"
                };
            }
            return result;


        }


        //删除
        public ActionResult DeleteApprove(int id)
        {

            JsonResult result = new JsonResult();
            db = new Management();
            IEmployees iem = db.IEmployees.FirstOrDefault(m=>m.Id==id);
            db.IEmployees.Remove(iem);
            db.SaveChanges();
          
            result.Data = new
            {
                State = "1",
                Message = "删除成功"
            };
            return result;
        }


        //弹窗
        public ActionResult Entry()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Entry(Entrypractice ta)
        {
            //接收AJAX的数据
            int q = Convert.ToInt32(Request.Form["PositionId"]);
            int w = Convert.ToInt32(Request.Form["DepartmentId"]);
            string e = Request.Form["JobNumber"];
            int r = Convert.ToInt32(Request.Form["TrialPay"]);

            //将值赋到新的实体类MODEL
            ta.PositionId = q;
            ta.DepartmentId = w;
            ta.JobNumber = e;
            ta.TrialPay = r;
            ta.Perid = Convert.ToInt32(Session["View_oneID"]); ;

            Management db = new Management();
            JsonResult result = new JsonResult();
            db = new Management();
            //使用拼接字符串的方式，将要执行的存储过程名称以及参数拼接在一起
            StringBuilder procString = new StringBuilder();
            procString.Append("Tf_insert");
            procString.Append(" @JobNumbers,");
            procString.Append("@TrialPays,");
            procString.Append("@PositionId,");
            procString.Append("@DepartmentId,");
            procString.Append("@cview");

            SqlParameter[] sq = {
                    new SqlParameter("@JobNumbers",ta.JobNumber),
                    new SqlParameter("@TrialPays",ta.TrialPay),
                    new SqlParameter("@PositionId",ta.PositionId),
                    new SqlParameter("@DepartmentId",ta.DepartmentId),
                     new SqlParameter("@cview",ta.Perid),
              };
            int a = db.Database.ExecuteSqlCommand(procString.ToString(), sq);
            if (a > 0)
            {
                result.Data = new
                {
                    State = "1",
                    Message = "添加成功"
                };
            }
            else
            {
                result.Data = new
                {
                    State = "2",
                    Message = "添加失败"
                };
            }
            return result;
        }


        /// <summary>
        /// 获取职位
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Position()
        {
            db = new Management();
            List<Postion> li = new List<Postion>();
            li = db.Postion.ToList();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                data = JsonTool.JsonSerializer(li)
            };
            return result;
        }
        /// <summary>
        /// 获取部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Department()
        {
            db = new Management();
            List<Department> li = new List<Department>();
            li = db.Department.ToList();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                data = JsonTool.JsonSerializer(li)
            };
            return result;
        }


    }
}